package com.gxm.base.io.本地IO.面试题;

import org.junit.Test;

import java.io.*;

public class 对象序列化 {

    @Test
    public void test01() throws IOException {
        //对象输出流
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("D://obj")));
        objectOutputStream.writeObject(new User("zhangsan", 100));
        objectOutputStream.close();
    }

    @Test
    public void test02() throws IOException, ClassNotFoundException {
        //对象输入流
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("D://obj")));
        User user = (User)objectInputStream.readObject();
         System.out.println(user);
         System.out.println(user.getName());
         objectInputStream.close();
    }
}

class User implements Serializable{
    private String name;
    private int age;
    User(String name,int age){
        this.name = name;
        this.age = age;
    }

    public String getName(){
        return this.name;
    }
}
